<template>
  <div class="in">
    <!-- 面包屑 -->
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>库存管理</el-breadcrumb-item>
      <el-breadcrumb-item>入库</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片 -->
    <el-card>
      <!-- 搜索和添加 -->
      <el-row type="flex" class="row-bg" justify="space-around">
        <el-col :span="16">
          <el-button icon="el-icon-circle-plus-outline" @click="addInDialogVisible = true">入库</el-button>
        </el-col>
        <el-col :span="8">
          <el-input placeholder="请输入内容" v-model="inInfo.query" clearable @clear="getInList">
            <el-button slot="append" icon="el-icon-search" @click="getInList"></el-button>
          </el-input>
        </el-col>
      </el-row>
      <!-- 入库列表 -->
      <el-table :data="inList" style="width: 100%" border>
        <el-table-column prop="id" label="编号">
        </el-table-column>
        <el-table-column prop="ycName" label="药材名称">
        </el-table-column>
        <el-table-column prop="inAmount" label="入库数量(克)">
        </el-table-column>
        <el-table-column prop="ycPrice" label="药材单价(元/克)">
        </el-table-column>
        <el-table-column prop="inPrice" label="药材总价(元/克)">
        </el-table-column>
        <el-table-column prop="inDate" label="入库日期">
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button type="warning" icon="el-icon-delete" @click="deleteIn(scope.row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="inInfo.pageNum"
        :page-sizes="[4, 6, 8, 9]" :page-size="inInfo.pageSize" layout="total, sizes, prev, pager, next, jumper" :total="total">
      </el-pagination>
    </el-card>
    <!-- 入库对话框 -->
    <el-dialog title="入库" :visible.sync="addInDialogVisible" width="30%" @close="addInDialogClosed">
      <el-form :model="addInForm" :rules="addInFormRules" ref="addInFormRef" label-width="100px">
        <el-form-item label="药材名称" prop="ycName">
          <el-input v-model="addInForm.ycName"></el-input>
        </el-form-item>
        <el-form-item label="入库数量" prop="inAmount">
          <el-input v-model="addInForm.inAmount" type="number"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="addInDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addIn">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
  export default {
    data() {
      return {
        inInfo: {
          query: '',
          pageNum: 1,
          pageSize: 4
        },
        inList: [],
        //总条数
        total: 0,
        //控制入库对话框显示
        addInDialogVisible: false,
        addInForm: {
          ycName: '',
          inAmount: 0
        },
        addInFormRules: {
          ycName: [{
            required: true,
            message: '请输入药材名称',
            trigger: 'blur'
          }],
          inAmount: [{
            required: true,
            message: '请输入药材入库数量',
            trigger: 'blur'
          }],
        }
      }
    },
    created() {
      this.getInList()
    },
    methods: {
      async getInList() {
        const {data: res} = await this.$http.post('/indata', this.inInfo)
        this.inList = res.data.list
        this.total = res.data.total
      },
      //监听显示条数变化
      handleSizeChange(size) {
        this.inInfo.pageSize = size
        this.getInList()
      },
      //监听页码变化
      handleCurrentChange(page) {
        this.inInfo.pageNum = page
        this.getInList()
      },
      //监听入库对话框关闭
      addInDialogClosed() {
        this.$refs.addInFormRef.resetFields()
      },
      //提交表单
      addIn() {
        this.$refs.addInFormRef.validate(async valid => {
          if (!valid) return
          const res = await this.$http.post('/indata/add', this.addInForm)
          console.log(res.data)
          if (res.data.status === false) {
            this.addInDialogVisible = false
            this.$message.error('入库失败，请先添加药材信息！')
            return
          }
          this.addInDialogVisible = false
          this.$message.success('入库成功！')
          this.getInList()
        })
      },
      //删除入库单
      async deleteIn(id) {
         const opRes = await this.$confirm('此操作将永久删除该药材信息, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).catch(err => err)
        if(opRes === 'confirm') {
          const { data: res } = await this.$http.get('/indel/' + id)
          this.$message.success('删除入库单成功！')
          this.getInList()
        }
      }
    }
  }
</script>

<style>
</style>
